/*
 * Copyright (C) 2018 The Asiainfo-Ability Authors
 *
 *      Licensed under the Apache License, Version 2.0 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 *      Unless required by applicable law or agreed to in writing, software
 *      distributed under the License is distributed on an "AS IS" BASIS,
 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *      See the License for the specific language governing permissions and
 *      limitations under the License.
 */

package org.asiainfo.ability.manager.utils;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import org.apache.commons.codec.binary.Hex;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * @Author: visen
 * @Date: 2018/1/16
 * @Description:
 */
public class SHAxxx {


    public static final String getSHA256(String input) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest(input.getBytes("UTF-8"));
            return Hex.encodeHexString(hash);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static void main(String args[]) {
        try {
            YAMLMapper yamlMapper = new YAMLMapper();
            JarFile jarFile = new JarFile("D:\\ability-crm-1.0.jar");

            Enumeration<JarEntry> jarEntrys = jarFile.entries();
            Properties props = new Properties();
            System.out.println(System.getProperty("user.dir"));
            while (jarEntrys.hasMoreElements()) {
                JarEntry entry = jarEntrys.nextElement();

                // 简单的判断路径，如果想做到像Spring，Ant-Style格式的路径匹配需要用到正则。
                String name = entry.getName();
                if("BOOT-INF/classes/config/application.yml".equals(name)) {
                    JsonNode node = yamlMapper.readTree(jarFile.getInputStream(entry) );
                    node.toString();
                }
                System.out.println(name);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        String appSecret = "C9YAFINEIBGM2N3IF7FTAPDDW6PE4BWNC9YAFINEIBGM2N3IF7FTAPDDW6PE4BWN";
        String appKey = "4011DF00E47F518A";
        String timestamp = "20150917123020123";
        String tag = appSecret + appKey + timestamp;
        System.out.println("tag:" + tag);
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest(tag.getBytes("UTF-8"));
            String output = Hex.encodeHexString(hash);
            System.out.println("SHA256_tag:" + output);
            System.out.println("签名后长度：" + output.length());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        System.out.println(sf.format(new Date()));
    }

    public static String md5(String info) {

        try {
            // 得到一个信息摘要器
            MessageDigest digest = MessageDigest.getInstance("md5");
            byte[] result = digest.digest(info.getBytes());
            StringBuffer buffer = new StringBuffer();
            // 把每一个byte 做一个与运算 0xff;
            for (byte b : result) {
                // 与运算
                int number = b & 0xff;// 加盐
                String str = Integer.toHexString(number);
                if (str.length() == 1) {
                    buffer.append("0");
                }
                buffer.append(str);
            }
            return buffer.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }


}
